package com.example.blogServer.interceptor;

import com.example.blogServer.properties.JwtProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.desktop.PreferencesHandler;

/**
 * 拦截器
 * @author Spark
 * @version 1.0
 */
@Component
@Slf4j
//该类需要继承拦截器处理器HandlerInterceptor
public class JwtUserTokenInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtProperties jwtProperties;

    //crl+o快速找到自己想要重写的方法
    //此处重写的preHandle会在请求发生前进行拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if(!(handler instanceof HandlerMethod)){
            //拦截到的不是动态方法直接放行
            return true;
        }

        //在请求头中获取令牌
        String token = request.getHeader(jwtProperties.getUserTokenName());

        //校验令牌


        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
